﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class api_redeemCodeList : PageBase
{
    redeemCodeList bll = new redeemCodeList();
    string baseName = "兑换码";
    protected void Page_Load(object sender, EventArgs e)
    {
        ResponseData rd = isLogin();
        if (rd.code != 1)
        {
            Json(rd);
            return;
        }
        string action = Request["action"];
        switch (action)
        {
            case "GetList"://查询数据 
                GetList();
                break;
            case "Insert"://新增一条 
                Insert();
                break;
            case "Update"://更新一条 
                Update();
                break;
            case "Delete"://删除一条 
                Delete();
                break;
            case "DeleteBatch"://批量删除 
                DeleteBatch();
                break;
            case "Createcode"://生成兑换码 
                Createcode();
                break;
            default:
                break;
        }
    }

    public string GeiGuid()
    {
        Guid guid = Guid.NewGuid();
        string guidPart = guid.ToString("N").ToUpper();
        return guidPart;
    }

    private void Createcode()
    {
        ResponseData rd = new ResponseData();
        try
        {
            Hashtable h_p = getPostParameter_json();
            object gid = h_p["gid"];
            object codeNum = h_p["codeNum"];
            if (codeNum == null || codeNum.ToString() == "")
            {
                rd.code = 0;
                Json(rd);
                return;
            }
            Hashtable h = new Hashtable();
            h["gid"] = gid;
            int sum = 0;
            for (int i = 0; i < Convert.ToInt32(codeNum); i++)
            {
                h["guid"] = GeiGuid();
                string sql = string.Format("insert into redeemCodeList(gid,code,isUse,useTime,createTime)" +
                    "values(@gid, @guid, '0', '', CONVERT(varchar(100), GETDATE(), 120));");
                int ii = new SQLHelper("getnum").Execute(sql, h);
                if (ii > 0)
                {
                    sum++;
                }
            }
            rd.code = sum > 0 ? 1 : 0;
            rd.message = sum > 0 ? "生成" + sum + "条" + baseName + "成功！" : "生成" + sum + "条" + baseName + "失败!";
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    private void DeleteBatch()
    {
        ResponseData rd = new ResponseData();
        try
        {
            Hashtable h_p = getPostParameter_json();
            object idList = h_p["idList"];
            if (idList == null || idList.ToString() == "")
            {
                rd.code = 0;
                rd.message = "请传id";
                Json(rd);
                return;
            }
            ArrayList idListArr = h_p["idList"] as ArrayList;
            string str = "";
            foreach (object item in idListArr)
            {
                str += item.ToString() + ",";
            }
            str = str.TrimEnd(',');
            int i = bll.DeleteBatch(str);
            rd.code = i > 0 ? 1 : 0;
            rd.message = i > 0 ? "删除" + i + "条" + baseName + "成功！" : "删除" + i + "条" + baseName + "失败!";
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    private void Delete()
    {
        ResponseData rd = new ResponseData();
        try
        {
            Hashtable h_p = getPostParameter_json();
            object id = h_p["id"];
            if (id == null || id.ToString() == "")
            {
                rd.code = 0;
                rd.message = "请传id";
                Json(rd);
                return;
            }
            int i = bll.Delete(id.ToString());
            rd.code = i > 0 ? 1 : 0;
            rd.message = i > 0 ? "删除一条" + baseName + "成功！" : "删除一条" + baseName + "失败!";
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    private void Insert()
    {
        ResponseData rd = new ResponseData();
        try
        {

            Hashtable h_p = getPostParameter_json();
            Hashtable h = new Hashtable();
            h["userNum"] = h_p["userNum"];
            Hashtable h_model = bll.GetModel(h_p);
            int i = 0;
            if (h_model.Count == 0)
            {
                h_p["updateTime"] = ""; //更新时间
                h_p["createTime"] = GetDateTimeString(); //创建时间
                i = bll.Insert(h_p);
            }
            rd.code = i > 0 ? 1 : 0;
            rd.message = i > 0 ? "添加" + baseName + "成功！" : "添加" + baseName + "失败!";
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }
    private void Update()
    {
        ResponseData rd = new ResponseData();
        try
        {
            Hashtable h_p = getPostParameter_json();
            if (h_p["id"] == null) // 新增
            {
                Hashtable h_param = new Hashtable();
                h_param["code"] = h_p["code"];
                h_param["userNum"] = h_p["userNum"];
                Hashtable h_model = bll.GetModel(h_param);
                int i = 0;
                if (h_model.Count == 0)
                {
                    h_p["updateTime"] = ""; //更新时间
                    h_p["createTime"] = GetDateTimeString(); //创建时间
                    i = bll.Insert(h_p);
                }
                rd.code = i > 0 ? 1 : 0;
                rd.message = i > 0 ? "添加" + baseName + "成功！" : "添加" + baseName + "失败! 可能已存在";
            }
            else // 修改
            {
                h_p["updateTime"] = GetDateTimeString(); //更新时间
                if (h_p.Contains("createTime"))
                {
                    h_p.Remove("createTime");
                }
                int i = bll.Update(h_p);
                rd.code = i > 0 ? 1 : 0;
                rd.message = i > 0 ? "更新" + baseName + "成功！" : "更新" + baseName + "失败!";
            }
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    public void GetList()
    {
        ResponseData rd = new ResponseData();
        try
        {
            #region 查询条件
            Hashtable h_user = CurrentLoginInfo();
            Hashtable h_p = getPostParameter_json();
            int pageSize = h_p["pageSize"] == null ? 10 : int.Parse(h_p["pageSize"].ToString());//每页显示行
            int pageNo = h_p["pageNum"] == null ? 1 : int.Parse(h_p["pageNum"].ToString());//页码，起始值 1
            int total = 0; string where = string.Empty;//where条件
            Hashtable h_parameters = new Hashtable();
            object gid = h_p["gid"];
            if (gid != null && gid.ToString() != "")
            {
                h_parameters["gid"] = gid;
                where += string.Format(" and gid = @gid");
            }
            object code = h_p["code"];
            if (code != null && code.ToString() != "")
            {
                h_parameters["code"] = "%" + code + "%";
                where += string.Format(" and code like @code");
            }
            #endregion
            ArrayList list = bll.GetList(pageSize, pageNo, out total, where, h_parameters);
            Hashtable h_res = new Hashtable();
            h_res.Add("list", list);
            h_res.Add("total", total);//数据总数
            rd.code = 1;
            rd.message = "获取列表成功";
            rd.data = h_res;
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }
}